package com.zhang.exception;

import com.zhang.constants.WebConstant;
import com.zhang.modules.api.annotation.ApiPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "错误转发")
@Controller
@RequestMapping("/api/error")
@ApiPermission(anonymous = true)
public class ErrorController {


    @ApiPermission(anonymous = true)
    @ApiOperation("处理shiro抛出的错误")
    @RequestMapping("/shiroFilter")
    public void error(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = (String) request.getAttribute(WebConstant.EXCEPTION_MESSAGE);
        String exceptionType = (String) request.getAttribute(WebConstant.EXCEPTION_TYPE);
        if (exceptionType.equals(WebConstant.API_EXCEPTION)){
            throw new ApiException(message);
        }else{
            throw new AuthenticationException(message);
        }
    }
}
